package com.controller.sys;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.EasyuiUtils;
import com.common.StringUtils;
import com.dao.sys.SysLogMapper;
import com.dao.sys.SysManageMapper;
import com.dao.sys.SysModuleMapper;
import com.model.sys.SysModule;
import com.service.sys.SysLogService;
import com.service.sys.SysService;

@Controller
public class ModelManageController {
	static final Logger logger = LoggerFactory.getLogger(ModelManageController.class);

	@Autowired
	private SysManageMapper sysManageMapper;
	@Autowired
	private SysLogMapper sysLogMapper;
	@Autowired
	private SysLogService sysLogService;
	@Autowired
	private SysModuleMapper sysModuleMapper;
	@Autowired
	private SysService sysService;

	@RequestMapping("/sys/ModuleManage")
	public Map<String, String> getMap() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("key1", "value-1");
		map.put("key2", "value-2");
		map.put("key3", "value-2");
		return map;
	}

	@RequestMapping(value = "/ModuleManage/ModuleListJson_Tree", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String ModuleListJson_Tree(HttpServletRequest request) {
		try {
			List<SysModule> list = sysModuleMapper.selectAll();
			String slist = EasyuiUtils.getTreeGridJson(list, "moduleid", "parentmoduleid", "0");
			// System.out.println(slist);
			logger.info("modulelist:{}", slist);
			// request.setAttribute("addLists", list);
			return slist;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return "test/test";
		}
	}

	@RequestMapping(value = "/ModuleManage/ModuleListJson_combotree", produces = "text/json;charset=UTF-8")
	@ResponseBody
	public String ModuleListJson_combotree(HttpServletRequest request) {
		try {
			List<SysModule> list = sysModuleMapper.selectAll();
			String slist = EasyuiUtils.getCobTreeJson(list, "moduleid", "modulename", "parentmoduleid", "0", "", true);
//			System.out.println("ModuleListJson_combotree" + slist);
			logger.info("ModuleListJson_combotree:{}", slist);
			// request.setAttribute("addLists", list);
			return slist;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return "test/test";
		}
	}

	@RequestMapping(value = "/ModuleManage/AddModule", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String AddModule(HttpServletRequest request) {
		try {
			String resultString = "保存成功!";
			SysModule model = new SysModule();
			String czr = request.getParameter("czr");
			String id = request.getParameter("id");
			model.setModulename(request.getParameter("name"));
			model.setParentmoduleid(request.getParameter("pid"));
			model.setModuleurl(request.getParameter("murl"));
			model.setMtarget(request.getParameter("target"));
			model.setShow(request.getParameter("show"));
			model.setDisabled(request.getParameter("disabled1"));
			String sort = request.getParameter("sortby");
			if (sort == null || sort.equals("")) {
				sort = "0";
			}
			BigDecimal bd = new BigDecimal(sort);
			model.setSortby(bd);
			model.setMcomment(request.getParameter("comment"));
			model.setMpic("");
			model.setLocked("0");
			String actionString = request.getParameter("action");
			// TODO 涓嬮潰绌烘牸涔嬪墠涓轰贡鐮侊紝闇�瑕佸畾浣嶅埌搴曚箣鍓嶆槸浠�涔堝瓧绗�
			model.setActionstring(StringUtils.sideTrim(actionString.replace(' ', ','), ","));
			String[] list = model.getActionstring().split(",");
			if (StringUtils.IsRepeat(list)) {
				resultString = "模块已存在!";
				return resultString;
			}
			if (id == null || id.equals("")) {
				id = UUID.randomUUID().toString();
				model.setModuleid(id);
				resultString = sysService.addModule(model) > 0 ? "保存成功!" : "保存失败!";
				sysLogService.add(czr, "", "新增模块_" + id, "", "", "", "");
			} else {
				model.setModuleid(id);
				resultString = sysService.updateModule(model) > 0 ? "保存成功!" : "保存失败!";
				sysLogService.add(czr, "", "修改模块_" + id, "", "", "", "");
			}
			return resultString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();
		}
	}

	@RequestMapping(value = "/ModuleManage/DelModule", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String DelModule(HttpServletRequest request) {
		try {
			String resultString = "保存成功!";
			String id = request.getParameter("MODULEID");
			String czr = request.getParameter("czr");
			resultString = sysService.deleteModule(id) > 0 ? "保存成功!" : "保存失败!";
			sysLogService.add(czr, "", "删除模块_" + id, "", "", "", "");

			return resultString;
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("errorMessage", "" + e.getMessage());
			logger.error("errorMessage:{}", e.getMessage());
			return e.getMessage();
		}
	}

}
